using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameForKids
{

    public class Cell : DrawableGameComponent
    {
        public event EventHandler Clicked = delegate { };

        // hinh anh khi up va khi lat
        protected Texture2D upImg;
        protected Texture2D downImg;
        protected string _chart;
        protected SpriteFont font;

        //hinh chu nhat chua cell
        protected Rectangle rect;
        protected List<int> touchIds;

        // kiem tra hinh up hay mo
        public bool pressed = false;

        //hieu ung chop chop
        protected bool animated = false;
        protected float opacity = 1.0f;

        public bool Enabled { get; set; }
        public bool Visible { get; set; }

        protected SoundEffect sound;
        protected bool playOneTime = true;

        public Point Location
        {
            get { return rect.Location; }
            set { rect.Location = value; }
        }

        public Point Center
        {
            get { return rect.Center; }
            set
            {
                rect.X = value.X - rect.Width / 2;
                rect.Y = value.Y - rect.Height / 2;
            }
        }


        public float Opacity
        {
            get { return opacity; }
            set
            {
                opacity = value;
                Visible = opacity > 0.0f;
            }
        }

        public Cell(Game game,Texture2D up, Texture2D down, string s, SpriteFont f) : base (game)
        {
            upImg = up;
            downImg = down;
            _chart = s;
            rect = upImg.Bounds;
            font = f;
            // vi tri ban dau nam ngoai viewport de khong anh huong cac doi tuong
            Location = new Point(800, 480);
            touchIds = new List<int>();
            Enabled = true;
            Visible = true;

        }

        /// <summary>
        /// Enables the button
        /// </summary>
        public void Enable()
        {
            Enabled = true;
            opacity = 1.0f;
        }

        /// <summary>
        /// Disables the button
        /// </summary>
        public void Disable()
        {
            Enabled = false;
            opacity = 0.0f;
        }



        //kiem tra diem co nam trong khung chu nhat hay ko
        public bool Contains(Point position)
        {
            return rect.Contains(position);
        }

        //kiem tra diem co nam trong khung chu nhat hay ko
        public bool Contains(Vector2 position)
        {
            return Contains(new Point((int)position.X, (int)position.Y));
        }


        public virtual void Draw(SpriteBatch spriteBatch, int a)
        {
            rect.Width = a;
            rect.Height = a;
            Vector2 rectOfText = font.MeasureString(_chart);
            float xScale = (a / rectOfText.X) * 0.85f;
            float yScale = (a / rectOfText.Y) * 0.85f;
            float scale = Math.Min(xScale, yScale);
            int strWidth = (int)Math.Round(rectOfText.X * scale);
            int strHeight = (int)Math.Round(rectOfText.Y * scale);
            Vector2 postion = new Vector2();
            postion.X = (((a - strWidth) / 2) + rect.X);
            postion.Y = (((a - strHeight) / 2) + rect.Y);

            if (Visible)
            {
                if (pressed)
                {
                    switch (GameForKids.gameState)
                    {
                        case GameState.SoundMemory:
                            {
                                spriteBatch.Draw(downImg, rect, Color.White * opacity);
                                if (playOneTime)
                                {
                                    sound = this.Game.Content.Load<SoundEffect>("Sounds/sound" + int.Parse(_chart));
                                    sound.Play();
                                    playOneTime = false;
                                }
                            }
                            break;
                        default:
                            {
                                spriteBatch.Draw(downImg, rect, Color.White * opacity);
                                spriteBatch.DrawString(font, _chart, postion, Color.Black,
                                    0.0f, new Vector2(0, 0), scale, SpriteEffects.None, 0);
                            }
                            break;
                    }
                    
     
                }
                else
                {
                    spriteBatch.Draw(upImg, rect, Color.White * opacity);
                    playOneTime = true;
                }
            }
        }



        public void TouchEvent(TouchLocation touch)
        {
            switch (touch.State)
            {
                case TouchLocationState.Pressed:
                    Pressed(touch);
                    break;
                case TouchLocationState.Moved:
                    Moved(touch);
                    break;
                case TouchLocationState.Released:
                    Released(touch);
                    break;
            }
        }


        public void Pressed(TouchLocation touch)
        {
            if (!Enabled || !Visible)
                return;
            if (Contains(touch.Position))
            {
                if (!touchIds.Contains(touch.Id))
                    touchIds.Add(touch.Id);
                if (!pressed)
                    pressed = true;
                else
                    pressed = false;
            }
        }


        public void Moved(TouchLocation touch)
        {

        }


        public void Released(TouchLocation touch)
        {

        }
        public void reset()
        {
            pressed = false;
            Enabled = true;
            opacity = 1.0f;
        }
        public void hardReset()
        {
            pressed = false;
            Enabled = true;
            opacity = 1.0f;
            Location = new Point(800, 480);
        }

        // hieu ung chop chop
        public void Effect(float delaytime)
        {
            if (delaytime >= 200)
                opacity = 0.5f;
            if (delaytime >= 400)
                opacity = 1.0f;
            if (delaytime >= 600)
                opacity = 0.5f;
            if (delaytime >= 800)
                opacity = 1.0f;
        }
    }
}
